<?php

namespace app\selladmin\model;

use think\Model;
use think\Db;

/*
 * 销售管理员
 */

class Selladmin extends Model {

    const STATUS_1 = 1; //启用
    const STATUS_2 = 2; //禁用
    
    const LEVEL_0 = 0; //超级管理员
    const LEVEL_1 = 1; //总经理
    const LEVEL_2 = 2; //经理
    const LEVEL_3 = 3; //员工

    //管理员列表

    public function AdminList($pagenum,$status=1) {
        $id = session('S_ID');
        $row = Selladmin::where('id','=',$id)->field('id,level')->find();
        $level = $row['level'];
        if($level==1){
            $where['level'] =['in','2,3'];
        }elseif ($level == 2) {
            $where['pid'] = $id;
        }
        $where = isset($where)?$where:[];
        $AdminList = Selladmin::order("id asc")
                ->where('status','=',$status)
                ->where($where)
                ->paginate($pagenum);
        return $AdminList;
    }
    
    /*
     * 生成员工唯一码
     */
    public static function generateStaffCode($firstname)
    {
        $str='1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,4);
        $code = $firstname. $rands;

        $exist = Db::name('selladmin')
            ->where('code', '=', $code)
            ->find();
        if (is_null($exist)) {
            return $code;
        } else {
            self::generateStaffCode();
        }
    }

}
